<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=wFy50EAszGznyPoG7M3pHEUM"></script>
	<script type="text/javascript" src="${rootPath}/Common/Js/jquery.js"></script>
		<script type="text/javascript" src="${rootPath}/Common/Js/common.js"></script>
	<title>地址解析</title>
</head>
<body>	
	<form method="post" action="index!nearHospital" name="queryForm" id="queryForm">
		<input type="hidden" name="nearHospitals" value="" id="nearHospitals"/>
		<div style="margin-top: 0px;height:28px;">输入地址：<input type="text" name="address" id="address" value=""/>&nbsp;&nbsp;<input type="button" onclick="getPoint();" value="搜  索" style="width: 10%;height: 26px;color:#FFFFFF;background-color: #666699;border:0px;"/></div>
		<div style="width:650px;height:490px;border:1px solid gray;float:left;" id="allmap"></div>
		<div style="width:310px;height:490px;border:1px solid gray;border-left:0;float:left;overflow: auto;" id="r-result"></div>
	</form>
</body>
</html>
<script type="text/javascript">
	// 百度地图API功能
	
	var map = new BMap.Map("allmap");
	var point = new BMap.Point(116.404,39.915);
	map.centerAndZoom(point,12);
	//创建地址解析器实例
	var myGeo = new BMap.Geocoder();
	// 将地址解析结果显示在地图上,并调整地图视野
	//var index = 0;
	
	function getPoint(){		
		var address = document.getElementById("address").value;
		//var address = ["昆明穿心鼓楼","昆明大观楼","昆明山灞大厦"];
		map.clearOverlays();
		myGeo.getPoint(address, function(point){
			if (point) {
				map.centerAndZoom(point, 15);
				var icon = new BMap.Icon("${rootPath}/Themes/BlueStyle/image/edit/mark.png", new BMap.Size(32, 32), {
				    anchor: new BMap.Size(10, 30)
				});
				var mkr = new BMap.Marker(point, {
				    icon: icon
				});
				map.addOverlay(mkr);
				//map.addOverlay(new BMap.Marker(point));
				var circle = new BMap.Circle(point,2000,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.1, strokeOpacity: 0.1});  
				var local = new BMap.LocalSearch(map, {renderOptions: {map: map, panel: "r-result", autoViewport: false}});
				local.setSearchCompleteCallback(function (searchResult) {
					 var s=[]; 
					 for (var i = 0; i < searchResult.getCurrentNumPois(); i ++){
						s.push(searchResult.getPoi(i).title + "^" + searchResult.getPoi(i).address);
						 //s.push(searchResult.getPoi(i).point.lng + "^" + searchResult.getPoi(i).point.lat);
					 } 
					 document.getElementById("nearHospitals").value = s.join(";");	
					 //filter();					 
					 //map.removeTileLayer(local);
					 
				});
				var bounds = getSquareBounds(circle.getCenter(),circle.getRadius());  
				local.searchInBounds("医院",bounds);				
				map.addOverlay(circle);				
				
			}
			
		}, "中国");
	}
	
	/**
	 * 过滤百度定位的医院信息
	 */
	function filter() {
		var nearHospitals=$("#nearHospitals").val();
		var my=getJSON(nearHospitals);	
		var myArr = my.split("^");
		var adds=eval("("+myArr[0]+")");
		var reps=myArr[1].replace("[,","[");
		var rep=eval(reps);
    	for(var i = 0; i<adds.length; i++){
    		var marker = new BMap.Marker(adds[i]);
    		map.addOverlay(marker);
    		document.getElementById("r-result").innerHTML += "<p style='font-size:12px;'>" + (i+1) + "、<span style='font-weight:bold'>名称：</span>" + rep[i].name + "；<span style='font-weight:bold'>地址：</span>" + rep[i].address + "</p>";
    	    //map.setInfoWindowAdapter(this);
    	    var infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>名称：" + rep[i].name + "</p><br><p style='font-size:14px;'>地址：" + rep[i].address + "</p>");
    	    marker.info = infoWindow;
    	    marker.addEventListener("click", function () { this.openInfoWindow(this.info); });
    	}    	   		
	}
	
	
	/**
	 * 同步获取医院信息
	 */
	function getJSON(nearHospitals){
		var rel;
		$.ajax({
            type: "POST", //POST提交
            dataType: "text", //文本
            url: 'index!filterHospital', //请求地址
            data: "nearHospitals=" + nearHospitals,
            async: false,
            error: function (XMLHttpRequest, textStatus, errorThrown) { },
            success: function (msg){             	
            	rel=msg;
            }
        });
		try{
			return  rel;
		}catch(e){alert("非JSON格式"+e);}
	}
	
	function geocodeSearch(add,address){
	    if(index < address.length){
	        setTimeout(window.filter,300);
	    }
	    myGeo.getPoint(add, function(point){
	      if (point) {
	    	  map.centerAndZoom(point, 14);
	    	  //map.addOverlay(new BMap.Marker(point));
	    	  var diz = new BMap.Point(point.lng, point.lat);
				addMarker(diz,new BMap.Label(index+":"+add,{offset:new BMap.Size(20,-10)}));
	      }
	    }, "中国");
	}
	
	// 添加标注
	function addMarker(point,label){
		var marker = new BMap.Marker(point);
		map.addOverlay(marker);
		marker.setLabel(label);
	}

	
	/**
     * 得到圆的内接正方形bounds
     * @param {Point} centerPoi 圆形范围的圆心
     * @param {Number} r 圆形范围的半径
     * @return 无返回值   
     */ 
    function getSquareBounds(centerPoi,r){
        var a = Math.sqrt(2) * r; //正方形边长
      
        mPoi = getMecator(centerPoi);
        var x0 = mPoi.x, y0 = mPoi.y;
     
        var x1 = x0 + a / 2 , y1 = y0 + a / 2;//东北点
        var x2 = x0 - a / 2 , y2 = y0 - a / 2;//西南点
        
        var ne = getPoi(new BMap.Pixel(x1, y1)), sw = getPoi(new BMap.Pixel(x2, y2));
        return new BMap.Bounds(sw, ne);        
        
    }
    //根据球面坐标获得平面坐标。
    function getMecator(poi){
        return map.getMapType().getProjection().lngLatToPoint(poi);
    }
    //根据平面坐标获得球面坐标。
    function getPoi(mecator){
        return map.getMapType().getProjection().pointToLngLat(mecator);
    }
	
</script>